package utils;

import android.content.Context;
import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.util.UUID;

import kitwee.com.kanpad.MyMultiDexApplication;

/**
 * Created by sven on 2017/7/11.
 */

public class YunpaiUtils {
    private YunpaiUtils() {
        throw new UnsupportedOperationException("hey, buddy, easy...");
    }

    private static String yunpaiID = null;

    /**
     * 获取id
     */
    public static String getYunPaiId() {
        if (yunpaiID == null) {
            synchronized (DeviceUtils.class) {
                if (yunpaiID == null) {
                    return yunpaiID = getOnlyID(MyMultiDexApplication.getContext());
                }
            }
        }
        return yunpaiID;
    }

    private static String deviceKanBanID = null;
    public static String getOnlyID(Context context) {

        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            String serialnum = (String) (get.invoke(c, "ro.serialno", "unknown"));
            if (serialnum != null && !TextUtils.equals("", serialnum) && !TextUtils.equals("unknown", serialnum)) {
                deviceKanBanID = serialnum;
                return deviceKanBanID;
            } else {
                DeviceUuidFactory factory = new DeviceUuidFactory(context);
                UUID deviceUuid = factory.getDeviceUuid();
                System.out.println(deviceUuid.toString());
                String mac = getMac();
                if (mac != null && mac != "") {
                    String md5 = getMD5(deviceUuid.toString() + mac);
                    deviceKanBanID = md5;
                    return deviceKanBanID;
                } else {
                    String md5 = getMD5(deviceUuid.toString());
                    deviceKanBanID = md5;
                    return deviceKanBanID;
                }

            }
//ID,是只需要生成设备唯一Id
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
        return null;
    }

    static String getMac() {
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);


            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }


    public static String getMD5(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes("utf-8");
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
